#include "../mqserver/mq_exchange.hpp"
#include <gtest/gtest.h>

castlemq::ExchangeManager::ptr emp;

class ExchangeTest:public testing::Environment
{
    public:
        virtual void SetUp() override
        {
            emp=std::make_shared<castlemq::ExchangeManager>("./data/meta1.db");
        }

        virtual void TearDown() override
        {
            //emp->clear();
        }
};

TEST(ExchangeTest,insert_test)
{
    std::unordered_map<std::string, std::string> map {{"k1","v1"},{"k2","v2"},{"k3","v3"}};
    emp->declareExchange("exchange1",castlemq::ExchangeType::DIRECT,true,false,map);
    emp->declareExchange("exchange2",castlemq::ExchangeType::DIRECT,true,false,map);
    emp->declareExchange("exchange3",castlemq::ExchangeType::DIRECT,true,false,map);
    emp->declareExchange("exchange4",castlemq::ExchangeType::DIRECT,true,false,map);
    ASSERT_EQ(emp->size(),4);
}

TEST(ExchangeTest,select_test)
{
    ASSERT_EQ(emp->exsits("exchange1"),true);
    ASSERT_EQ(emp->exsits("exchange2"),true);
    ASSERT_EQ(emp->exsits("exchange3"),false);
    ASSERT_EQ(emp->exsits("exchange4"),true);
    castlemq::Exchange::ptr exp=emp->selectExchange("exchange2");
    ASSERT_EQ(exp->name,"exchange2");
    ASSERT_EQ(exp->durable,true);
    ASSERT_EQ(exp->auto_delete,false);
    ASSERT_EQ(exp->type,castlemq::ExchangeType::DIRECT);
    // ASSERT_EQ(exp->getArgs().c_str(),"k3=v3&k2=v2&k1=v1&");
    ASSERT_EQ(exp->getArgs(),std::string("k1=v1&k2=v2&k3=v3&"));
}

TEST(ExchangeTest,remove_test)
{
    emp->deleteExchange("exchange2");
    castlemq::Exchange::ptr exp=emp->selectExchange("exchange2");
    ASSERT_EQ(exp.get(),nullptr);
}

int main(int argc,char* argv[])
{
    testing::InitGoogleTest(&argc,argv);
    testing::AddGlobalTestEnvironment(new ExchangeTest);
    RUN_ALL_TESTS();
    return 0;
}